//
//  ElectricityDetail2ViewController.m
//  EasyToo
//
//  Created by linshengqin on 13-8-24.
//
//

#import "ElectricityDetail2ViewController.h"
#import "ElectricityPayViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "DeviceUtil.h"
@interface ElectricityDetail2ViewController ()

@end

@implementation ElectricityDetail2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
     [self.view setBackgroundColor:BackgroundColor];

    
    [self initData];
    [self initMainView];
    [self initTableView:cellLabelArray];
    [self initLabel:120];
    [self initButton:120];
}

-(void)initData
{
    electricity_model = [ElectricityModel sharedata];

    cellLabelArray = [[NSMutableArray alloc]initWithObjects:@"办理月份",@"本金",@"用电量",@"滞纳金",@"总金额", nil];
    info = electricity_model.electricFeeInfoArray[0];
}

-(void)initMainView
{
    _bgScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [DeviceUtil getScreenWidth], [DeviceUtil getScreenHeight])];
    [self.view addSubview:_bgScrollView];
    _bgScrollView.contentSize = CGSizeMake(320, 700);
    _bgScrollView.showsVerticalScrollIndicator = YES;
    
}

-(void) initTableView:(NSArray*)namelist
{
    /*
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, [DeviceUtil getScreenWidth], 80) style:UITableViewStylePlain];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [_bgScrollView addSubview:tableView];
     */
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 50, [DeviceUtil getScreenWidth], 80) style:UITableViewStylePlain];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [_bgScrollView addSubview:tableView];
/*
    for (int i  = 0; i < [namelist count]; i ++)
    {
        UIButton *bgBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        bgBtn.frame = CGRectMake(10, 46+i*40, 320, 40);
        [bgBtn setBackgroundImage:[UIImage imageNamed:@"flightticket1_03"] forState:UIControlStateNormal];
        [_bgScrollView addSubview:bgBtn];
        
        UIButton *delBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        delBtn.tag = i+300;
        delBtn.frame = CGRectMake(280, 10 50+i*90, 20, 20);
        [delBtn addTarget:self action:@selector(delEvent:) forControlEvents:UIControlEventTouchUpInside];
        [delBtn setBackgroundImage:[UIImage imageNamed:@"cp_ssshanchu"] forState:UIControlStateNormal];
        [bgBtn addSubview:delBtn];
        
        UILabel *pricelbl = [[[UILabel alloc]initWithFrame:CGRectMake(0, 10, 280, 30)]autorelease];
        pricelbl.backgroundColor = [UIColor clearColor];
        pricelbl.text = [namelist objectAtIndex:i];
        pricelbl.font = [UIFont fontWithName:@"Arial" size:12];   //设置内容字体和字体大小
        [bgBtn addSubview:pricelbl];
    }
 */
}


-(void) initLabel:(int)distance
{
    UILabel *label_user = [[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 30)]autorelease];
    [label_user setText:[NSString stringWithFormat:@"户号 :%@",electricity_model.account]];
    label_user.font = [UIFont fontWithName:@"Arial" size:12];   //设置内容字体和字体大小
    label_user.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_user];
    
    
    UIImage *image = [UIImage imageNamed:@"shurubi"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, distance+50, 10, 15);
    [_bgScrollView addSubview:imageView];
    
    UILabel *label_contact = [[[UILabel alloc]initWithFrame:CGRectMake(30,distance+40, 180, 30)]autorelease];
    [label_contact setText:[NSString stringWithFormat:@"填写联系方式"]];
    label_contact.backgroundColor = [UIColor clearColor];

    [_bgScrollView addSubview:label_contact];
    
    phoneNum = [[[UITextField alloc] initWithFrame:CGRectMake(30, distance+80, 250, 30)]autorelease];
    [phoneNum setBorderStyle:UITextBorderStyleBezel];
    phoneNum.placeholder = @"手机号码";
    phoneNum.borderStyle = UITextBorderStyleRoundedRect;
    phoneNum.delegate = self;
    phoneNum.returnKeyType = UIReturnKeyDone;
    [_bgScrollView addSubview:phoneNum];
    
    UILabel *label_tip = [[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMinY(phoneNum.frame)+30, 320, 40)]autorelease];
    label_tip.font = [UIFont fontWithName:@"Arial" size:10];   //设置内容字体和字体大小
    label_tip.lineBreakMode = UILineBreakModeTailTruncation;
    label_tip.numberOfLines = 0;

    label_tip.text = @"为确保您委托的业务顺利办理进行,请务必留下您的联系方式,业务办理完成后,我们的客服人员将会对通过您留下的电话号码通知您";
    label_tip.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:label_tip];
}

-(void) initButton:(int)distance
{
    UIImage *image = [UIImage imageNamed:@"select_button"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image] ;
    imageView.frame = CGRectMake(20, distance+150, 10, 10);
    [_bgScrollView addSubview:imageView];
    
    UILabel *label_pay = [[[UILabel alloc]initWithFrame:CGRectMake(30, distance+140, 180, 30)]autorelease];
    label_pay.text = @"选择支付方式:";
    label_pay.font = [UIFont fontWithName:@"Arial" size:14];   //设置内容字体和字体大小
    label_pay.backgroundColor = [UIColor clearColor];
    [_bgScrollView addSubview:label_pay];
    
    self.ylButton = [[UIButton alloc] initWithFrame:CGRectMake(105, distance+180, 100, 30)];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian1.png"] forState:UIControlStateNormal];
    [self.ylButton setBackgroundImage:[UIImage imageNamed:@"yinlian2.png"] forState:UIControlStateSelected];
    [self.ylButton addTarget:self action:@selector(ylButtonPick:) forControlEvents:UIControlEventTouchDown];
    [_bgScrollView addSubview:self.ylButton];
    
    
    //下一步按钮
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton setFrame:CGRectMake(20, distance+240, 280, 40)];
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];        //设置button在没有选中的时候显示的字体
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];         //设置button显示字体的大小
    [nextButton setBackgroundImage:[UIImage imageNamed:@"button_3"] forState:UIControlStateNormal];    //设置button背景显示图片
    [_bgScrollView addSubview:nextButton];
    [nextButton addTarget:self action:@selector(confirmMessge:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)zfbButtonPick:(id)sender
{
    [self.zfbButton setSelected:YES];
    [self.ylButton setSelected:NO];
    electricity_model.payType = 0;
}

-(void)ylButtonPick:(id)sender
{
    [self.zfbButton setSelected:NO];
    [self.ylButton setSelected:YES];
    electricity_model.payType = 1;
    
}

-(void)confirmMessge:(id)sender
{
    ElectricityPayViewController *detail = [[[ElectricityPayViewController alloc]init]autorelease];
    if( [phoneNum text] == nil || [phoneNum.text isEqualToString:@""] == YES)
    {
        UIAlertView *_baseAlert = [[[UIAlertView alloc]  initWithTitle:@"出错" message:@"请输入用户手机"  delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil]autorelease];
        [_baseAlert show];
    }
    else
    {
        [electricity_model.phoneNum setString:[phoneNum text]];
        [self.navigationController pushViewController:detail animated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];
    if(oneCell == nil)
    {
        oneCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mycell"];
        oneCell.frame = CGRectMake(0, 0, 300, 40) ;
        for(int i = 0 ; i < 5 ;i++)
        {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(oneCell.frame.size.width/5*i, 0, oneCell.frame.size.width/5, oneCell.frame.size.height)];
            label.textColor = [UIColor blackColor];
            label.textAlignment = NSTextAlignmentCenter ;
            label.tag = 170 + i;
            label.font = [UIFont fontWithName:@"Arial" size:12];
            label.layer.borderColor = [UIColor grayColor].CGColor;
            label.layer.borderWidth = 0.3 ;
            label.backgroundColor = [UIColor clearColor];
            [oneCell addSubview:label];
        }
    }
    for(int i = 0 ; i < 5 ;i++)
    {
        
        UILabel *label = (UILabel *)[oneCell viewWithTag:170 + i] ;
        if( indexPath.row == 0)
            label.text = [cellLabelArray objectAtIndex:i];
        else
        {
            //label.text = [cellLabelArray2 objectAtIndex:i];
            if( i == 0)
            {
                label.text = info.billMonth;
            }
            else if( i == 1)
            {
                float currentFee = [info.currentFee floatValue]/100;
                label.text = [NSString stringWithFormat:@"%.2f",currentFee];
            }
            else if( i == 2)
            {
                float consumption = [info.consumption floatValue]/100;
                label.text = [NSString stringWithFormat:@"%.2f",consumption];
            }
            else if( i == 3)
            {
                float laterFee = [info.laterFee floatValue]/100;
                label.text = [NSString stringWithFormat:@"%.2f",laterFee];
            }
            else if( i == 4)
            {
                float totalFee = [info.totalFee floatValue]/100;
                label.text = [NSString stringWithFormat:@"%.2f",totalFee];
            }

        }
        
    }
    oneCell.layer.borderColor = [UIColor grayColor].CGColor;
    return oneCell ;
    
}

-(void) dealloc
{
    [cellLabelArray release] ,cellLabelArray = nil;
    [cellLabelArray2 release] ,cellLabelArray2 = nil;
    
    [super dealloc];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
@end
